package com.example.myys.ui.base

import android.widget.Toast
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.NativeKeyEvent
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.example.myys.data.entity.DataBean
import com.example.myys.ui.theme.MyYsTheme

//列表子项
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ListItem(
    onClickItem: (dataBean: DataBean) -> Unit = {}, //点击事件
    onDeleteClick: (dataBean: DataBean) -> Unit = {}, //删除事件
    isDeleteAble: Boolean = false,//删除显示
    dataBean: DataBean = DataBean(),//加载数据
    index: Int? = null //序号
) {
    val context = LocalContext.current
    var isDelete by remember {
        mutableStateOf(false)
    }
    Surface(
        modifier = Modifier
            .width(300.dp)
            .height(180.dp)
            .onKeyEvent {
                if (it.nativeKeyEvent.keyCode == NativeKeyEvent.KEYCODE_DPAD_CENTER && it.nativeKeyEvent.isLongPress) {
                    isDelete = true
                    return@onKeyEvent true
                }
                false
            }
            .combinedClickable(
                onClick = {
                    if (!isDelete) {
                        Toast
                            .makeText(context, "默认点击", Toast.LENGTH_SHORT)
                            .show()
                        onClickItem(dataBean)
                    }
                },
                onLongClick = {
                    Toast
                        .makeText(context, "长按", Toast.LENGTH_SHORT)
                        .show()
                    isDelete = true
                }
            ),
        shape = MaterialTheme.shapes.medium,
    ) {
        Card {
            Row(
                modifier = Modifier
                    .background(MaterialTheme.colorScheme.primaryContainer)
            ) {
                Box(modifier = Modifier.padding(3.dp)) {
                    AsyncImage(
                        modifier = Modifier
                            .width(150.dp)
                            .fillMaxHeight(),
                        model = dataBean.cover,
                        contentDescription = dataBean.name,
                        contentScale = ContentScale.Crop
                    )
                    index?.let {
                        Text(
                            text = "${index}",
                            modifier = Modifier
                                .background(Color.Blue)
                                .padding(2.dp),
                            color = Color.White

                        )
                    }
                    if (dataBean.dataSourceName.isNotBlank()) {
                        Text(
                            text = dataBean.dataSourceName,
                            modifier = Modifier
                                .align(Alignment.TopStart)
                                .background(Color.Black.copy(alpha = 0.6f))
                                .padding(2.dp),
                            color = Color.White

                        )
                    }
                }

                Column(
                    modifier = Modifier
                        .padding(3.dp)
                        .fillMaxSize(),
                ) {
                    Text(
                        text = "${dataBean.name}",
                        style = MaterialTheme.typography.titleMedium,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                    Text(
                        text = "类型:${dataBean.category}",
                        style = MaterialTheme.typography.titleSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                    Text(
                        text = "状态:${dataBean.status}",
                        style = MaterialTheme.typography.titleSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                    Text(
                        text = "作者:${dataBean.author}",
                        style = MaterialTheme.typography.titleSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                    Text(
                        text = "发布时间:${dataBean.releaseTime}",
                        style = MaterialTheme.typography.titleSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                    Text(
                        text = "更新时间:${dataBean.updateTime}",
                        style = MaterialTheme.typography.titleSmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                    if (dataBean.browseTime.isNotBlank()) {
                        Text(
                            text = "观看时间:${dataBean.browseTime}",
                            style = MaterialTheme.typography.titleSmall,
                            maxLines = 1,
                            overflow = TextOverflow.Ellipsis,
                            textAlign = TextAlign.Left
                        )
                    }
                    Text(
                        text = "简介:${dataBean.desc}",
                        style = MaterialTheme.typography.bodyMedium,
                        maxLines = 6,
                        overflow = TextOverflow.Ellipsis,
                        textAlign = TextAlign.Left
                    )
                }
            }
        }
        if (isDelete && isDeleteAble) {
            AlertDialog(
                title = {
                    Text(text = "删除收藏")
                },
                text = {
                    Text(text = "是否删除")
                },
                onDismissRequest = {
                    isDelete = false
                },
                confirmButton = {
                    TextButton(
                        onClick = {
                            Toast.makeText(context, "开始删除", Toast.LENGTH_SHORT).show()
                            onDeleteClick(dataBean)
                            isDelete = false
                        }
                    ) {
                        Text("删除")
                    }
                },
                dismissButton = {
                    TextButton(
                        onClick = {
                            //isDelete = false
                        }
                    ) {
                        Text("返回键关闭")
                    }
                })
        }
    }

}

//排行子项
@Composable
fun PhItem(
    onClickItem: (dataBean: DataBean) -> Unit = {}, //点击事件
    dataBean: DataBean = DataBean(),//加载数据
    index: Int = 1,//序号
) {
    Card(
        onClick = { onClickItem(dataBean) }
    ) {
        Row(
            modifier = Modifier
                .background(MaterialTheme.colorScheme.primaryContainer)
                .padding(4.dp, 0.dp)
                .width(200.dp)
                .height(40.dp)

        ) {
            Text(
                text = "${index}.",
                style = MaterialTheme.typography.labelLarge,
                modifier = Modifier
                    .fillMaxHeight()
                    .wrapContentHeight(Alignment.CenterVertically),
            )
            Text(
                text = "${dataBean.name}",
                style = MaterialTheme.typography.labelMedium,
                modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight()
                    .wrapContentHeight(Alignment.CenterVertically),
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                textAlign = TextAlign.Left,
            )
            Text(
                text = "${dataBean.updateTime}",
                style = MaterialTheme.typography.labelSmall,
                modifier = Modifier
                    .fillMaxHeight()
                    .wrapContentHeight(Alignment.CenterVertically)
            )
        }
    }
}

@Composable
@Preview(showBackground = true)
fun show() {
    MyYsTheme {
        ListItem()
    }
}
